using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DARE
{
    public class COctreeItemModel : IPartitionerItemModel
    {

    #region fields

        private Color m_color;
        private VertexPositionColor[] m_vertices;
        private VertexBuffer m_vertexBuffer;
        private BasicEffect m_basicEffect;

    #endregion

    #region ctor

        /// <summary>
        /// Create an octree item model.
        /// This is the collision view box of all items in the octree.
        /// </summary>
        /// <param name="aabb">Bounding box.</param>
        public COctreeItemModel(BoundingBox aabb)
        {
            m_color = Color.Green;
            m_basicEffect = CDare.Instance.XnaBasicEffect;
            SetUpVertices(aabb);
        }

    #endregion

    #region methods

        /// <summary>
        /// Set all the vertices of the model.
        /// </summary>
        /// <param name="aabb">Bounding box.</param>
        public void SetUpVertices(BoundingBox aabb)
        {
            m_vertices = new VertexPositionColor[24];

            float xMin = -(aabb.Max.X - aabb.Min.X) / 2;
            float xMax = (aabb.Max.X - aabb.Min.X) / 2;
            float yMin = -(aabb.Max.Y - aabb.Min.Y) / 2;
            float yMax = (aabb.Max.Y - aabb.Min.Y) / 2;
            float zMin = -(aabb.Max.Z - aabb.Min.Z) / 2;
            float zMax = (aabb.Max.Z - aabb.Min.Z) / 2;

            int count = 0;

            // front
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMin, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMin, zMin), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMax, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMax, zMin), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMin, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMax, zMin), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMin, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMax, zMin), m_color);

            // back
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMin, zMax), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMin, zMax), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMax, zMax), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMax, zMax), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMin, zMax), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMax, zMax), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMin, zMax), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMax, zMax), m_color);

            // link
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMin, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMin, zMax), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMin, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMin, zMax), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMax, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMin, yMax, zMax), m_color);

            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMax, zMin), m_color);
            m_vertices[count++] = new VertexPositionColor(new Vector3(xMax, yMax, zMax), m_color);

            // set the vertex buffer
            m_vertexBuffer = new VertexBuffer(CDare.Instance.GraphicsDevice, typeof(VertexPositionColor), m_vertices.Length, BufferUsage.WriteOnly);
            m_vertexBuffer.SetData(m_vertices);
        }

        /// <summary>
        /// Draw the octree item model.
        /// </summary>
        /// <param name="position">Draw the model at this position.</param>
        public void Draw(Vector3 position)
        {
            CDare.Instance.GraphicsDevice.SetVertexBuffer(m_vertexBuffer);

            m_basicEffect.VertexColorEnabled = true;
            m_basicEffect.LightingEnabled = false;

            m_basicEffect.World = Matrix.CreateTranslation(position);
            m_basicEffect.View = CDare.Instance.SceneMgr.ActiveScene.ActiveRender3D.CameraMgr.ActiveCamera.View;
            m_basicEffect.Projection = CDare.Instance.SceneMgr.ActiveScene.ActiveRender3D.CameraMgr.ActiveCamera.Projection;

            foreach (EffectPass pass in m_basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                CDare.Instance.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, m_vertices.Length / 2);
            }
        }

    #endregion

    }
}
